const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database({
  env: 'gtss-2b1812'
})
exports.main = async(event, context) => {
  if (event.Sf == '1') {
    var stu = await db.collection('Student').where({
      stuName: event.Xm,
      _id: event.Zh
    }).get()
    if (stu.data.length == 1 && stu.data[0].stuPas == event.Mm) {
      return {
        result: 1,
        dept: stu.data[0].stuDept,
        userState:stu.data[0].stuState
      }
    } else {
      return {
        result: 0
      }
    }
  }
  if (event.Sf == '2') {
    var tea = await db.collection('Teacher').where({
      teaName: event.Xm,
      _id: event.Zh
    }).get()
    if (tea.data.length == 1 && tea.data[0].teaPas == event.Mm) {
      return {
        result: 2,
        dept: tea.data[0].teaDept,
        userState: tea.data[0].teaState
      }
    } else {
      return {
        result: 0
      }
    }
  }
  if (event.Sf == '3') {
    var adm = await db.collection('Administrator').where({
      admName: event.Xm,
      _id: event.Zh
    }).get()
    if (adm.data.length == 1 && adm.data[0].admPas == event.Mm) {
      return {
        result: 3,
        dept: adm.data[0].admDept
      }
    } else {
      return {
        result: 0
      }
    }
  }
}